package com.liu.mybatisPlus.wm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.mybatisPlus.wm.entity.base.WmRivenAttributes;
import com.liu.mybatisPlus.wm.entity.base.WmRivenAttributesKeyWord;
import com.liu.mybatisPlus.wm.mapper.WmRivenAttributesKeyWordMapper;
import com.liu.mybatisPlus.wm.service.WmRivenAttributesKeyWordService;
import com.liu.mybatisPlus.wm.service.WmRivenAttributesService;
import com.liu.qqRobot.wm.entity.qo.AddWmRivenAttributesKeyWordQO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuChengLong
 * @since 2025-04-01
 */
@Service
public class WmRivenAttributesKeyWordServiceImpl extends ServiceImpl<WmRivenAttributesKeyWordMapper, WmRivenAttributesKeyWord> implements WmRivenAttributesKeyWordService {

    @Resource
    private WmRivenAttributesService wmRivenAttributesService;

    @Override
    public void add(List<AddWmRivenAttributesKeyWordQO> qoList) {
        // 校验关键词是否存在
        List<WmRivenAttributesKeyWord> insertList = new ArrayList<>();
        for (AddWmRivenAttributesKeyWordQO qo : qoList) {
            WmRivenAttributes wmRivenAttributes = wmRivenAttributesService.getById(qo.getRivenAttributesId());
            if (wmRivenAttributes == null) {
                continue;
            }
            QueryWrapper<WmRivenAttributesKeyWord> wmRivenAttributesKeyWordQueryWrapper = new QueryWrapper<>();
            wmRivenAttributesKeyWordQueryWrapper.eq("key_word", qo.getKeyWord());
            WmRivenAttributesKeyWord wmRivenAttributesKeyWord = super.getOne(wmRivenAttributesKeyWordQueryWrapper);
            if (wmRivenAttributesKeyWord != null) {
                continue;
            }
            wmRivenAttributesKeyWord = new WmRivenAttributesKeyWord();
            wmRivenAttributesKeyWord.setKeyWord(qo.getKeyWord());
            wmRivenAttributesKeyWord.setId(qo.getRivenAttributesId());
            insertList.add(wmRivenAttributesKeyWord);
        }
        super.saveBatch(insertList);
    }

}
